package com.youtui.manager.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youtui.common.constant.SystemConstant;
import com.youtui.common.service.base.impl.BaseServiceImpl;
import com.youtui.manager.mapper.OpterMapper;
import com.youtui.manager.model.Opter;
import com.youtui.manager.model.OpterExample;
import com.youtui.manager.model.OpterExample.Criteria;
import com.youtui.manager.service.IOpterService;

@Service
public class OpterServiceImpl extends BaseServiceImpl implements IOpterService {

	@Autowired
	OpterMapper opterMapper;

	@Override
	public Opter findOpterById(Long id) {

		if (id == null) {
			return null;
		}
		return opterMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageInfo<Opter> findOpterPageList(Opter opter, int page , int rows) {
		PageHelper.startPage( page ,  rows);
		OpterExample example = new OpterExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(opter.getLoginname())) {
			criteria.andLoginnameLike("%" + opter.getLoginname() + "%");
		}
		if (StringUtils.isNotBlank(opter.getRealname())) {
			criteria.andRealnameLike("%" + opter.getRealname() + "%");
		}

		criteria.andIdGreaterThan(1l); /// 不查询超级管理员账号

		criteria.andStatusEqualTo(SystemConstant.status_effect);
		List<Opter> list = opterMapper.selectByExample(example);
		PageInfo<Opter> pageinfo = new PageInfo<Opter>(list);
		return pageinfo;

	}

	@Override
	public Opter findOpterByLoginName(String name) {
		OpterExample example = new OpterExample();
		Criteria criteria = example.createCriteria();
		criteria.andLoginnameEqualTo(name);
		List<Opter> list = opterMapper.selectByExample(example);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Opter clear(Opter opter) {
		if (opter != null) {
			opter.setPwd("");
		}
		return opter;
	}

	@Override
	public int add(Opter opter) {
		return opterMapper.insert(opter);
	}

	@Override
	public int update(Opter opter) {
		if (opter.getId() == null) {
			return 0;
		}
		return opterMapper.updateByPrimaryKeySelective(opter);
	}

}
